package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.CustomerInfo;
import com.fun_ordering.service.CustomerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 顾客信息表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/customerInfo")
public class CustomerInfoController {

	@Autowired
	private CustomerInfoService customerInfoService;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<CustomerInfo> findAll(){
		return customerInfoService.findAll();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return customerInfoService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param customerInfo
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody CustomerInfo customerInfo){
		try {
			customerInfoService.add(customerInfo);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param customerInfo
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody CustomerInfo customerInfo){
		try {
			customerInfoService.update(customerInfo);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param cusId
	 * @return
	 */
	@RequestMapping("/findOne")
	public CustomerInfo findOne(String cusId){
		return customerInfoService.findOne(cusId);
	}

	/**
	 * 批量删除
	 * @param cusIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(String [] cusIds){
		try {
			customerInfoService.delete(cusIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param customerInfo
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody CustomerInfo customerInfo, int page, int rows  ){
		return customerInfoService.findPage(customerInfo, page, rows);
	}

}
